﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityBLL;
using WorkFlowBLL;

namespace QLCF.Forms.admin
{
    public partial class frmLog : Form
    {
        public frmLog()
        {
            InitializeComponent();
            dateFrom.Format = DateTimePickerFormat.Custom;
            dateFrom.CustomFormat = "dd/MM/yyyy";
            dateTo.Format = DateTimePickerFormat.Custom;
            dateTo.CustomFormat = "dd/MM/yyyy";
            bindAction();            
        }

        private void bindAction()
        {
            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem(-1, "Tất cả"));
            items.Add(new ListItem(1, "Thêm mới"));
            items.Add(new ListItem(2, "Sửa"));
            items.Add(new ListItem(3, "Xóa"));
            cbxAction.ValueMember = "Value";
            cbxAction.DisplayMember = "text";
            cbxAction.DataSource = items;
        }
        private bool bindPage = true;
        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindPage = true;
            bindData(1);
        }
        private void bindData(int cur)
        {
            CtrAdmin ctra = new CtrAdmin();
            grvLog.AutoGenerateColumns = false;
            int? totalRecord = 0;
            int pageSize = 50;
            grvLog.DataSource = ctra.LogSearch(txtUserName.Text, txtFunctionName.Text, Convert.ToInt16(cbxAction.SelectedValue), dateFrom.Value, dateTo.Value, cur, pageSize, ref totalRecord);
            var totalPage = 1;
            if (totalRecord > 0)
            {
                totalPage = (int)Math.Floor((totalRecord.Value - 1.0) / pageSize) + 1;
            }
            lblPage.Text = "Trang " + cur + "/" + totalPage;

            if (bindPage)
            {
                bindPage = false;
                List<ListItem> items = new List<ListItem>();
                for (int i = 1; i <= totalPage; i++)
                    items.Add(new ListItem(i, i.ToString()));
                cbxPage.ValueMember = "Value";
                cbxPage.DisplayMember = "text";
                cbxPage.DataSource = items;                
            }
            enableNextPrev();
        }
        private void cbxPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindData(Convert.ToInt32(cbxPage.SelectedValue));
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            var cur = Convert.ToInt32(cbxPage.SelectedValue);
            cbxPage.SelectedValue = ++cur;
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            var cur = Convert.ToInt32(cbxPage.SelectedValue);
            cbxPage.SelectedValue = --cur;
        }

        private void enableNextPrev()
        {
            var cur = Convert.ToInt32(cbxPage.SelectedValue);
            if (cur < 2)
                btnPrev.Enabled = false;
            else
                btnPrev.Enabled = true;
            if (cur > cbxPage.Items.Count - 1)
                btnNext.Enabled = false;
            else
                btnNext.Enabled = true;
        }
    }
}
